// pages/me/me.js
import {
  post,
  validStringType,
  showToast,
  navigateTo
} from '../../utils/util.js';
import Request from '../../utils/request.js'
let app = getApp();
let store = app.store;


Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '--',
    mobile: '--',
    menus: [
      {
        name: '就诊人管理',
        icon: '/images/me/card-fill.png',
        link: './manage/index'
      },
      {
        name: '问诊记录',
        icon: '/images/me/inquiry-record.png',
        link: '/pages/inquiry/index/index'
      },
      {
        name: '缴费记录',
        icon: '/images/me/pay-record.png',
        link: '/pages/me/payRecord/payRecord'
      },
      {
        name: '关注的医生',
        icon: '/images/me/heart-fill.png',
        link: '/pages/home/doctorList/doctorList?entryID=2'
      },
      // 齐齐哈尔小程序，勿删
      // {
      //   name: '门诊患者满意度调查表',
      //   icon: '/images/me/pay-record.png',
      //   link: '/pages/me/questionnaire/outp'
      // },
      // {
      //   name: '住院患者满意度调查表',
      //   icon: '/images/me/pay-record.png',
      //   link: '/pages/me/questionnaire/inp'
      // },
      {
        name: '轮椅摆渡车预约记录',
        icon: '/images/me/pay-record.png',
        link: '/pages/me/wheelchairRecord/wheelchairRecord'
      },
      {
        name: '救护车预约记录',
        icon: '/images/me/ambulance.png',
        link: '/pages/me/ambulanceRecord/ambulanceRecord'
      },
      {
        name: '配送地址管理',
        icon: '/images/me/location-fill.png',
        link: '/pages/me/address/address'
      },
      {
        name: '意见反馈',
        icon: '/images/me/feedback.png',
        link: '/pages/me/feedback/feedback'
      },
      {
        name: '关于我们',
        icon: '/images/home-active.png',
        link: '/pages/me/about/about'
      }
    ]
  },

  formatIphone(tel) {
    if (!tel) return '--';
    return tel.substr(0, 3) + '****' + tel.substr(7);
  },
  getUserInfo() {
    let self = this;

    wx.showLoading({
      title: '获取用户信息...',
    })
    Request.getRequest('/api/wxUser/params', {
      id: store.getState().systemLoginData.imId
    }).then(data => {
      if (data.code == 1) {
        // 注册成功
        console.log("获取用户信息成功");

        self.setData({
          name: data.data[0].userName,
          mobile: self.formatIphone(data.data[0].mobile)
        })
      } else {
        // 给出本地出错提示
        showToast('error', data.msg || '获取用户信息错误')
      }
      wx.hideLoading()
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // this.getUserInfo();
    let mobile = store.getState().systemLoginData.mobile || wx.getStorageSync('mobile')
    this.setData({
      mobile: this.formatIphone(mobile)
    })
  },

  settingHandler: function() {
    store.dispatch({
      type: 'clear_LoginData'
    });
    wx.navigateTo({
      url: '/pages/login/login',
    });
  },

  gopage: (evt) => {
    navigateTo({
      url: evt.currentTarget.dataset.url
    });
  },

  gotab: (evt) => {
    wx.switchTab({
      url: evt.currentTarget.dataset.url
    });
  },

  showLoginDialog() {
    wx.showModal({
      content: '该功能需要登录后才可以使用，是否前往登录页面？',
      success(res) {
        if (res.confirm) {
          store.dispatch({
            type: 'clear_LoginData'
          });
          wx.reLaunch({
            url: '/pages/login/login',
          });
        } else if (res.cancel) {
          wx.switchTab({
            url: '/pages/home/index/index'
          });
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (!store.getState().systemLoginData.imId) {
      this.showLoginDialog();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})